package com.zj.jdbc3Batis.tools;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BeanListHandler
 * @Description: 将结果集转换成bean对象的list集合的处理器
 * @author: 孤傲苍狼
 * @date: 2014-10-5 下午12:00:06
 *
 */
public class BeanListHandler extends AbstractResultHandler {


    public BeanListHandler(Class<?> clazz) {
        super(clazz);
    }

    public Object handler(ResultSet rs) {
        try{
            List<Object> list = new ArrayList<Object>();
            boolean isWrap=isWrapClass(clazz);
            while(rs.next()){
                ResultSetMetaData  metadata = rs.getMetaData();
                int count = metadata.getColumnCount();
                if (!isWrap&& count > 1) {
                    throw new RuntimeException("查询列数与结果类型不匹配");
                }
                list.add(isWrap?putResultToPOJOType(rs,metadata):putResultToSimpleType(rs));
            }
            return list;
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
